<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\GoodsInsertRequest;

class GoodsController extends Controller
{
   /*
        商品列表
   */
        public function getIndex(Request $request)
        {   
            //获取每页显示几条
            $num = $request->input('num',10);
            //判断是否有查询条件
            if($request->input('keywords')){
                 
                $goods = DB::select('select * from shop_goods where gname like "%'.$request->input('keywords').'%"');
            }else{
                $goods = DB::table('shop_goods')->get();
            }
            // dd($goods);
            //获取参数列表
            $list = $request->all();
            
            //解析模板 根目录为resources下的views
            return view('admin.goods.index',['goods'=>$goods,'list'=>$list]);
        }
     	/*
        	商品添加页面
   		*/
        public function getAdd()
        {

        	$cates=CateController::getCates();
        	return view('admin.goods.add',['cates'=>$cates]);
        }

        public function postInsert(GoodsInsertRequest $request)
        {
             //提取数据
            $data=$request->only(['gname','tid','price','vcnt','cnt','scnt','press','author','number','ctime','goodscon']);
            //处理token
            $data['token']=str_random(50);
            
            //调用函数处理头像上传
            $data['gpic'] = $this->upload($request,'gpic');

            // dd($data);
            //执行数据插入操作
            $res = DB::table('shop_goods')->insert($data);
            if($res){
                //成功 到列表页
                return redirect('/admin/goods/index')->with('success','用户添加成功');
            }else{
                return back()->with('error','用户添加失败');
            }
        	//dd($request->all());
        }
        /*
            封装方法 进行头像上传处理
        */
        private function upload($request,$filename)
        {
            if($request->hasFile($filename)){
                //获取文件后缀名
                $suffix = $request->file($filename)->getClientOriginalExtension();
                //随机文件名
                $name = md5(time().rand(1,99999));
                $request->file($filename)->move('./uploads/', $name.'.'.$suffix);
                return '/uploads/'.$name.'.'.$suffix;
            }
        }

        /*
            封装方法 进行ajax删除操作
        */
        public function postDelete(Request $request)
        {
            // echo 'delete';
            $gid = $request->input('gid');
            //删除用户
            // $res = DB::table('shop_goods')->delete($gid);
            $res = DB::delete("delete from shop_goods where gid=".$gid);
            echo $res;
        }
        /*

            封装方法进行修改显示
        */
        // public function getEdit($id) 这种为第一种方法 index中注释的是其配套
        public function getEdit(Request $request)
        {
            $gid = $request->input('gid');

            $cates=CateController::getCates();
            
            //查询这个用户

            $goods = DB::table('shop_goods')->where('gid',$gid)->first();
            // dd($goods);
           //解析模板
            return view('admin/goods/edit',['goods'=>$goods,'cates'=>$cates]);
        }
        /*
            进行数据修改
        */
        public function postUpdate(Request $request)
        {
            $data = $request->only(['gname','tid','price','vcnt','cnt','scnt','press','author','number','ctime','goodscon']);
            //判断是否有头像上传
            if($request->hasFile('gpic')){
                $data['gpic'] = $this->upload($request,'gpic');
            }
            //获取id
            $gid = $request->input('gid');

            //进行数据修改操作
            $res = DB::table('shop_goods')->where('gid',$gid)->update($data);

            if($res){
                //成功 到列表页
                return redirect('/admin/goods/index')->with('success','用户修改成功');
            }else{
                return back()->with('error','用户修改失败');
            }

        }
}
